package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.reggie.bean.Dish;
import com.heima.reggie.bean.DishSi;
import com.heima.reggie.bean.PageParam;
import com.heima.reggie.common.R;
import com.heima.reggie.dto.DishDto;
import com.heima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService service;

    @PostMapping
    @ApiOperation(value ="按条件分页查询")
    public R selectByPage(@RequestBody PageParam pageParam) {
        IPage<DishDto> dishIPage = service.selectByPage(pageParam);
        if (dishIPage != null) {
            return R.success(dishIPage);
        } else {
            return R.error("查询失败！");
        }
    }

    @PostMapping("/add")
    @ApiOperation(value ="添加菜品")
    public R add(@RequestBody DishDto dishDto) {
        int add = service.add(dishDto);
        if (add > 0) {
            return R.success("添加成功！");
        } else {
            return R.error("添加失败！");
        }

    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value ="删除菜品")
    public R deleteById(@PathVariable List<Long> ids) {
        int i = service.deleteByIds(ids);
        if (i > 0) {
            return R.success("删除成功！");
        } else {
            return R.error("删除失败！");
        }
    }

    @GetMapping("/status")
    @ApiOperation(value ="更新菜品状态")
    public R updateStatus(DishSi dishSi) {
        System.out.println(dishSi);
        int i = service.updateStatus(dishSi.getStatus(),dishSi.getId());
        if (i > 0) {
            return R.success("更新状态成功！");
        } else {
            return R.error("更新状态失败！");
        }
    }

    @PutMapping
    @ApiOperation(value ="更新菜品")
    public R updateDish(@RequestBody DishDto dishDto) {
        int i = service.updateDish(dishDto);
        if (i > 0) {
            return R.success("更新成功！");
        } else {
            return R.error("更新失败！");
        }
    }

    @GetMapping("/list")
    @ApiOperation(value ="查询菜品")
    public R selectDishList(DishDto dishDto) {
        System.out.println("---------");
        System.out.println(dishDto.getStatus());
        List<DishDto> dishes = service.selectDishList(dishDto);
        if (dishes != null) {
            return R.success(dishes);
        } else {
            return R.error("查询菜品错误！");
        }
    }
}
